﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace GameEngine.Scenes
{
    public class InputManager
    {
        #region SingletonPattern
        static InputManager instance;
        private InputManager()
        {
        }
        public static InputManager getInstance()
        {
            if (instance == null)
                instance = new InputManager();
            return instance;
        }
        #endregion
        #region Attributes
        KeyboardState oldKeyboardState, newKeyboardState;
        MouseState oldMouseState,newMouseState;
        int lastClickTime = 10000;
        #endregion
        #region Functionality
        public void update(GameTime gt)
        {
            if (isDoubleClick())
                lastClickTime = 100000;
            else
            if (newMouseState.LeftButton != ButtonState.Pressed && oldMouseState.LeftButton != ButtonState.Released)
                lastClickTime = 0;
            oldKeyboardState = newKeyboardState;
            newKeyboardState = Keyboard.GetState();
            oldMouseState = newMouseState;
            newMouseState = Mouse.GetState();
            lastClickTime += gt.ElapsedGameTime.Milliseconds;

        }
        public bool isKeyDown(Keys k)
        {
            return newKeyboardState.IsKeyDown(k);
        }

        public bool isKeyUp(Keys k)
        {
            return !newKeyboardState.IsKeyDown(k);
        }

        public bool isNewKeyPressed(Keys k)
        {
            return (newKeyboardState.IsKeyDown(k) && !oldKeyboardState.IsKeyDown(k));
        }
        public bool isNewKeyReleased(Keys k)
        {
            return (!newKeyboardState.IsKeyDown(k) && oldKeyboardState.IsKeyDown(k));
        }
        public bool isNewClick()
        {
            return (newMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton == ButtonState.Released);
        }
               
        public bool isNewClickReleased()
        {
            return (newMouseState.LeftButton != ButtonState.Pressed && oldMouseState.LeftButton != ButtonState.Released);
        }
        public bool isDoubleClick()
        {
            return (isNewClickReleased() && lastClickTime < 500);
        }
        #endregion

    }
}
